﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using Microsoft.Win32;
using System.IO;
using System.Windows.Forms;

namespace Create_License
{
    public class XuLy
    {
        //LICENSE
        public static string KeySQL = "Key";
        public static string pathSQL = "Software\\MicrosoftSQLServer\\License";
        //END

        public static string Key = "Key";
        public static string path1 = "Software\\Manangersoft\\License";
        //public static string path2 = "Software\\Manangerrunsoft\\License";

        #region "   Registry_Set"
        public static Boolean Registry_Set(string KeyName, object Value,string path)
        {
            try
            {
                //RegistryKey rk = Registry.LocalMachine;
                RegistryKey sk1 = Registry.CurrentUser.CreateSubKey(path);//Application.ProductName
                sk1.SetValue(KeyName.ToUpper(), Value);

                //string a = Registry_Get(KeyName);

                return true;

            }
            catch (Exception e)
            {
                return false;
            }
        }
        #endregion

        #region "   Registry_Get"
        public static string Registry_Get(string KeyName,string path)
        {
            //RegistryKey rk = Registry.LocalMachine;
            RegistryKey sk1 = Registry.CurrentUser.OpenSubKey(path);//Application.ProductName

            try
            {
                if (sk1 != null)
                    return sk1.GetValue(KeyName.ToUpper()) + "";
            }
            catch (Exception ex)
            {
                string ss = ex.Message;
            }
            return null;
        }
        #endregion

        #region Get ID HDD
        public string GetSerialNumber1()
        {
            // ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
            string Serial = "";
            //string[] array = new string[searcher.Get().Count];
            //int i = 0;
            foreach (ManagementObject wmi_HD in searcher.Get())
            {
                //if (wmi_HD["SerialNumber"] != null)
                //{
                //    if (wmi_HD["SerialNumber"].ToString() != "")
                //    {
                //        Serial = wmi_HD["SerialNumber"].ToString();
                //        return Serial;

                //    }
                //    //array[i] = wmi_HD["SerialNumber"].ToString();
                //    //i++;
                //}
                if (wmi_HD["Signature"] != null)
                {
                    if (wmi_HD["Signature"].ToString() != "")
                    {
                        Serial = wmi_HD["Signature"].ToString();
                        return Serial;

                    }
                    //array[i] = wmi_HD["SerialNumber"].ToString();
                    //i++;
                }
            }
            return Serial;
        }
        public string[] GetSerialNumber()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            //string Serial = "";
            string[] array = new string[searcher.Get().Count];
            int i = 0;
            foreach (ManagementObject wmi_HD in searcher.Get())
            {
                if (wmi_HD["SerialNumber"] != null)
                {
                    array[i] = wmi_HD["SerialNumber"].ToString();
                    i++;
                }
            }
            return array;
        }

        public static string GetSerialNumber_str()
        {
           // ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
            string Serial = "";
            //string[] array = new string[searcher.Get().Count];
            int i = 0;
            foreach (ManagementObject wmi_HD in searcher.Get())
            {
                //if (wmi_HD["SerialNumber"] != null)
                //{
                //    if (wmi_HD["SerialNumber"].ToString() != "")
                //    {
                //        Serial = wmi_HD["SerialNumber"].ToString();
                //        return Serial;
 
                //    }
                //    //array[i] = wmi_HD["SerialNumber"].ToString();
                //    //i++;
                //}
                if (wmi_HD["Signature"] != null)
                {
                    if (wmi_HD["Signature"].ToString() != "")
                    {
                        Serial = wmi_HD["Signature"].ToString();
                        return Serial;

                    }
                    //array[i] = wmi_HD["SerialNumber"].ToString();
                    //i++;
                }
            }
            return Serial;
        }

        #endregion

        #region "   MaHoa"
        public string MaHoa(string Khoa)
        {
            byte[] utf16 = Encoding.Unicode.GetBytes(Khoa);
            return Convert.ToBase64String(utf16);
        }
        #endregion

        #region "   GiaiHoa"
        public string GiaiMa(string Khoa)
        {
            string str = "";
            try
            {
                byte[] utf = Convert.FromBase64String(Khoa);
                str = Encoding.Unicode.GetString(utf) + "";
            }
            catch (Exception)
            {

                return "";
            }
            return str;//.Replace(Khoa);
        }
        #endregion

        public static byte CreateFiletxt(string CDKey)
        {
            byte tempCreateFile = 0;
            string PathS = Application.StartupPath;
            string FileName = "System.txt";
            PathS = PathS + @"\"+FileName;
            try
            {
                tempCreateFile = 0;
                if (!(File.Exists(PathS)))
                {
                    StreamWriter St = new StreamWriter(PathS, false, System.Text.Encoding.Unicode);
                    St.WriteLine(gfEnCodeText("Mã hóa thông tin."));
                    St.WriteLine(gfEnCodeText("ncvgf45a09sda58888"));
                    St.WriteLine(gfEnCodeText("ca01av09876526a000"));
                    St.WriteLine(gfEnCodeText(CDKey));
                    St.WriteLine(gfEnCodeText("bfcr987asd09871234"));
                    St.Close();
                }
                else
                {
                    File.Delete(PathS);
                    if (!(File.Exists(PathS)))
                    {
                        StreamWriter St = new StreamWriter(PathS, false, System.Text.Encoding.Unicode);
                        St.WriteLine(gfEnCodeText("Mã hóa thông tin."));
                        St.WriteLine(gfEnCodeText("ncvgf45a09sda58888"));
                        St.WriteLine(gfEnCodeText("ca01av09876526a000"));
                        St.WriteLine(gfEnCodeText(CDKey));
                        St.WriteLine(gfEnCodeText("bfcr987asd09871234"));
                        St.Close();
                    }
                }
                tempCreateFile = 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Tạo File đăng nhập thất bại.", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return tempCreateFile;
        }

        #region 1.Tao File License

        public static string gfEnCodeText(string pStrText)
        {
            string tempgfEnCodeText = null;
            string strTemp = "";
            int intCoutn = 0;
            tempgfEnCodeText = "";

            try
            {


                for (intCoutn = 1; intCoutn <= pStrText.Length; intCoutn++)
                {
                    strTemp = strTemp + ((char)(System.Convert.ToInt32(pStrText[intCoutn - 1]) + 87)).ToString();
                }

                tempgfEnCodeText = strTemp;

            }
            catch (Exception ex)
            {
                MessageBox.Show("Mã hóa không thành công." + System.Environment.NewLine + "mdlFile - gfEnCodeText " + System.Environment.NewLine + ex.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {

            }

            return tempgfEnCodeText;
        }

        public string gfDeCodeText(string pStrText)
        {
            string tempgfDeCodeText = null;
            string strTemp = "";
            int intCoutn = 0;
            tempgfDeCodeText = "";

            try
            {

                for (intCoutn = 1; intCoutn <= pStrText.Length; intCoutn++)
                {
                    strTemp = strTemp + ((char)(System.Convert.ToInt32(pStrText[intCoutn - 1]) - 87)).ToString();
                }

                tempgfDeCodeText = strTemp;

            }
            catch (Exception ex)
            {
                MessageBox.Show("Giải mã không thành công." + System.Environment.NewLine + "mdlFile - gfDeCodeText " + System.Environment.NewLine + ex.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {

            }

            return tempgfDeCodeText;
        }

        public static byte CreateFile(string MatMa, string NgayBD, string NgayKT,string solan, string PathS)
        {
            byte tempCreateFile = 0;
            //string PathS = Application.StartupPath;
            //FileName = "License.txt";
            try
            {
                tempCreateFile = 0;
                if (!(File.Exists(PathS)))
                {
                    StreamWriter St = new StreamWriter(PathS, false, System.Text.Encoding.Unicode);
                    St.WriteLine(gfEnCodeText("Mã hóa thông tin."));
                    St.WriteLine(gfEnCodeText(MatMa));
                    St.WriteLine(gfEnCodeText(NgayBD));
                    St.WriteLine(gfEnCodeText(NgayKT));
                    St.WriteLine(gfEnCodeText("Số lần chạy chương trình_"+solan));
                    St.WriteLine(gfEnCodeText("Số lần đã chạy chương trình_" + 0));
                    St.Close();
                }
                else
                {
                    File.Delete(PathS);
                    if (!(File.Exists(PathS)))
                    {
                        StreamWriter St = new StreamWriter(PathS, false, System.Text.Encoding.Unicode);
                        St.WriteLine(gfEnCodeText("Mã hóa thông tin."));
                        St.WriteLine(gfEnCodeText(MatMa));
                        St.WriteLine(gfEnCodeText(NgayBD));
                        St.WriteLine(gfEnCodeText(NgayKT));
                        St.WriteLine(gfEnCodeText("Số lần chạy chương trình_"+solan));
                        St.WriteLine(gfEnCodeText("Số lần đã chạy chương trình_" + 0));
                        St.Close();
                    }
                }
                tempCreateFile = 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Tạo File đăng nhập thất bại.", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return tempCreateFile;
        }

        public static byte CreateFile_1(string MatMa, string NgayBD, string NgayKT, string solan,string slsd)
        {
            string PathS = Application.StartupPath + @"\License.txt";
            byte tempCreateFile = 0;
            try
            {
                tempCreateFile = 0;
               
                if (!(File.Exists(PathS)))
                {
                    StreamWriter St = new StreamWriter(PathS, false, System.Text.Encoding.Unicode);
                    St.WriteLine(gfEnCodeText("Mã hóa thông tin."));
                    St.WriteLine(gfEnCodeText(MatMa));
                    St.WriteLine(gfEnCodeText(NgayBD));
                    St.WriteLine(gfEnCodeText(NgayKT));
                    St.WriteLine(gfEnCodeText(solan));
                    St.WriteLine(gfEnCodeText("Số lần đã chạy chương trình_" + slsd));
                    St.Close();
                }
                tempCreateFile = 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Tạo File đăng nhập thất bại.", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return tempCreateFile;
        }

        #endregion

        #region 2.Chuyển đổi ANHLQ 29.09.2011

        public static string ChuyenDoi(string Chuoi)
        {
            string Str = "";
            int cd = Chuoi.Length;
            for (int i = 0; i < cd; i++)
            {
                switch (Chuoi[i])
                {
                    case 'A': Str += "0"; break;
                    case 'B': Str += "1"; break;
                    case 'C': Str += "2"; break;
                    case 'D': Str += "3"; break;
                    case 'E': Str += "4"; break;
                    case 'F': Str += "5"; break;
                    case 'G': Str += "6"; break;
                    case 'H': Str += "7"; break;
                    case 'I': Str += "8"; break;
                    case 'J': Str += "9"; break;
                    default: Str += Chuoi[i];
                        break;
                }
            }

            return Str;
        }

        #endregion

    }
}
